package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.CommercialDTO;
import com.quanquan.management.request.CommercialRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.Request;

import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: FloorService
 * @Author: xiaoqianh丶
 * @Date: 2019-04-09 18:27
 * @Version: V1.0
 */
@Service
public class CommercialService {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    public PageDTO<CommercialDTO> listCommercials(PageRequestBody pageRequestBody) {
        String json = Request.postJson(storeUrl+"commercial/query_management", pageRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<CommercialDTO> pageDTO = null;
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }


    public boolean addCommercial(CommercialRequest requestBody) {

        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "/commercial/add_commercial", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (null != data) {
                return true;
            }
        }
        return false;
    }

    public CommercialDTO findById(String id) {
        CommercialRequest requestBody = new CommercialRequest();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "commercial/query_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        CommercialDTO commercialDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            commercialDTO = data.toJavaObject(CommercialDTO.class);
        }
        return commercialDTO;
    }

    public boolean update(CommercialRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "commercial/update_commercial", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean delete(String id) {
        CommercialRequest requestBody = new CommercialRequest();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "commercial/remove_commercial", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

    public List<CommercialDTO> list() {
        String json = Request.postJson(storeUrl + "commercial/com_list",null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        List<CommercialDTO> commercialDTOS = null;
        if (0 == resCode) {
            JSONArray data = jsonObject.getJSONArray("data");
            commercialDTOS = data.toJavaList(CommercialDTO.class);
        }
        return commercialDTOS;
    }

}
